package com.itheima.reggie.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements com.itheima.reggie.service.DishService {
    //依赖注入dishMapper和dishFlavorMapper
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 添加菜品信息
     *
     * @param dishDto
     * @return boolean
     */
    @Override
    @Transactional
    public boolean save(DishDto dishDto) {
        //完善信息
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        //调用方法添加菜品信息
        Integer flag = dishMapper.save(dishDto);//成功添加后就有id

        //便利口味添加到口味表完善信息
        List<DishFlavor> dishFlavorsList = dishDto.getFlavors().stream().map((dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            return dishFlavor;
        })).collect(Collectors.toList());
        //调用dao层添加数据
        Integer flag2 = dishFlavorMapper.save(dishFlavorsList);
        if (flag > 0 && flag2 > 0) {
            return true;
        }
        return false;
    }

    /**
     * 连表查询分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findAll2(Integer page, Integer pageSize, String name) {
        //分页查询加工
        PageHelper.startPage(page, pageSize, name);
        List<DishDto> dishDtoList = dishMapper.findAll2(name);
        PageInfo<DishDto> pageInfo = new PageInfo<>(dishDtoList);

        //封装成page发送给前端
        Page<DishDto> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        //返回结果
        return pageObj;
    }

    /**
     * 分表查询分页数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findAll(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Dish> dishList = dishMapper.findAll(name);
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);
        List<DishDto> dishDtoList = dishList.stream().map((dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Long categoryId = dishDto.getCategoryId();
            Category category = categoryMapper.findById(categoryId);
            dishDto.setCategoryName(category.getName());
            return dishDto;
        })).collect(Collectors.toList());
        Page<DishDto> pageObj = new Page<>();
        pageObj.setRecords(dishDtoList);
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    /**
     * 根据id查询指定菜品和口味信息
     *
     * @param id
     * @return DishDto
     */
    @Override
    public DishDto findById(Long id) {
        return dishMapper.findById1(id);
    }

    /**
     * id获取查询菜品列表数据
     *
     * @param categoryId
     * @return List<Dish>
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        return dishMapper.findByCategoryId(categoryId);
    }

    /**
     * 修改菜品信息
     *
     * 记住千万记住开启事务
     *
     * @param dishDto
     * @return boolean
     */
    @Transactional
    @Override
    public boolean update(DishDto dishDto) {
        //完善数据
        dishDto.setUpdateTime(LocalDateTime.now());
        //调用修改业务方法修改菜品信息
        dishMapper.update(dishDto);
        //删除dish_id已有的口味信息
        dishFlavorMapper.delete(dishDto.getId());
        //遍历添加口味信息
        List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map((flavor -> {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());
        //调用业务层方法添加口味信息
        dishFlavorMapper.save(dishFlavorList);
        return true;
    }

    /**
     * 根据id查询菜品和菜品的口味数据
     *
     * @param categoryId
     * @param status
     * @return list
     */
    @Override
    public List<DishDto> findByCategoryIdAndStatus(Long categoryId, Integer status) {
        //调用mapper根据id查询口味和菜品数据
        List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status);
        //对数据进行加工,封装到DishDto中
        List<DishDto> dishDtoList = dishList.stream().map((dish -> {
            //封装到DishDto中
            DishDto dishDto = new DishDto();
            //将dish给dishDto
            BeanUtils.copyProperties(dish,dishDto);
            //调用dishFlavor方法
            List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishDto.getId());
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        })).collect(Collectors.toList());
        return dishDtoList;
    }


}
